import * as rq from 'request-promise';

import env from './../config/env';
import loginUtils from './../core/LoginUtils';


export class CkService {

    /**
     * 销售出库查询
     * @param res 
     */
    async queryOutStoreBill(res, authorization) {
        let checkNum: number = await loginUtils.checkUser(authorization);
        if (checkNum == 101) {
            return { code: checkNum };
        }

        var billcode: string = res.billcode;

        //防止SQL注入攻击
        var reg = /^[a-zA-Z0-9]{1,20}$/;
        if(!reg.test(billcode)) {
            return { 
                rtcode: 2,
                rtmsg: "输入的格式不正确"
            };
        }

        var input = {
            code: billcode
        };
        var url = `${env.testUrl}/service/QueryOutStoreBill`;

        console.log('call:', url);
        console.log('input:', input);

        let json: any = await rq.post(url,
            {
                form: input,
                json: true
            }
        )

        console.log('output:', json);

        if (json.rtcode == 1) {
            //单据号 billcode
            //单据日期
            let dbilldate: string = json.dbilldate;
            //客户名称
            let custname: string = json.custname;
            //商品信息
            let details: any = json.details;
            //hts
            let hts = json.hts;
            //拣货状态
            let status = json.status;

            let detail_list: any[] = [];
            details.map((item, i) => {
                detail_list[i] = {
                    invcode: item.invcode,//商品编码
                    invname: item.invname,//商品名称
                    measname: item.measname,//计量单位
                    noutnum: item.noutnum,//应发数量
                    cgeneralbid: item.cgeneralbid,//单据行Id
                    cscode: item.cscode,//货位编码
                    csname: item.csname,//货位名称
                    realnum: item.realnum//占存数量
                }
            });

            return {
                rtcode: 1,
                rtmsg: json.rtmsg,
                billcode: billcode,
                dbilldate: dbilldate,
                custname: custname,
                hts: hts,
                status: status,//拣货状态
                detail_list: detail_list
            }
        }

        return json;
    }

    /**
     * 销售出库签字
     * @param res 
     * @param authorization 
     */
    async WriteBackOutBill(res, authorization) {

        let checkNum: number = await loginUtils.checkUser(authorization);
        if (checkNum == 101) {
            return { code: checkNum };
        }
        console.log('res______________', res);

        let user_info = loginUtils.decode(authorization);
        let code: string = res.billcode;
        let usercode: string = user_info.username;
        // let hts: string = res.hts;
        // console.log('hts', hts);
        //新改
        let details: any = res.details;
        let status: string = res.status;
        console.log('__________________', status);
        console.log('__________________', details);
        console.log('_usercode_________________', usercode);
        let date: any = new Date().getTime();

        /* let json = await rq.get(`${env.testUrl}/service/WriteBackOutBill?code=`
            + code + '&usercode=' + usercode + '&hts=' + hts, { json: true }); */

        //更新销售出库状态（新启用字段，非单据状态）    供 PDA捡货完成时调用
        let json = await rq.post(`${env.testUrl}/service/UpdStatusOutBill`, {
            body: {
                code: code,
                usercode: usercode,
                details: details,
                status: status
            },
            json: true
        })

        console.log('json__________________', json);
        return json;
    }


    /**
     * 取消
     * 回写实发数量
     * @param res 
     */
    async writeOutNum(res, authorization) {

        let checkNum: number = await loginUtils.checkUser(authorization);
        if (checkNum == 101) {
            return { code: checkNum };
        }
        console.log('res______________', res);

        let billcode: string = res.billcode;
        let detail_list: any = res.detail_list;

        let details: any[] = [];
        detail_list.map((item, i) => {
            details[i] = {
                noutnum: item.realNum || 0,//实发数量
                cgeneralbid: item.cgeneralbid//单据行Id
            }
        });

        let json = await rq.post(`${env.testUrl}/service/WriteOutNum`,
            {
                body: {
                    code: billcode,
                    details: details
                },
                json: true
            }
        )

        return json;
    }


}


let ckService = new CkService();
export default ckService;